
import java.util.Arrays;



public class MyHead {
    int[] elem;
    int useSize;

    public MyHead() {
        this.elem = new int[10];
    }

    public void init(int[] arrays){
        for(int i=0;i<arrays.length;i++){
            this.elem[i]=arrays[i];
            this.useSize++;
        }
    }


    public void show(){
        int sum=0;
        int add=1;
        for(int i=0;i<useSize;i++) {
            if (i+1==add){
                System.out.println(elem[i]);
            sum++;
            add+=(int) (Math.pow(2,sum));
        }
            else
                System.out.print(elem[i]+" ");
        }
    }
    //向上调整创建大根堆（小根堆只需要改变比较符号）
    public void createHeap(){
        for(int parent=(useSize-2)/2;parent>=0;parent--){
            siftDown(parent,useSize);
        }
    }


    public void siftDown(int parent,int useSize){
        int a=parent*2+1;
       while (a<useSize){
           if(a+1<useSize&&elem[a]<elem[a+1]){
               a++;
           }
           if(elem[a]>elem[parent]){
               swap(parent,a);
               //交换一次后要重排a下的子树(因为换下来的·值可能很小)，直到parent与子树不再调整或者parent无子树（a越界）
               parent=a;
               a=parent*2+1;
           }
           else {
               break;
           }
       }
    }

    //交换函数
    public  void swap(int parent,int a ){
        int tmp=elem[parent];
        elem[parent]=elem[a];
        elem[a]=tmp;
    }

    //增加元素
    public void offer(int val){
        if(isFull()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
        elem[useSize]=val;
        siftUp( useSize);
        useSize++;
    }

    //向上交换
    public  void siftUp(int c){
        int parent=(c-1)/2;
        while (parent>=0){
            if(elem[parent]<elem[c]) {
                swap(parent, c);
                c = parent;
                parent = (c - 1) / 2;
            }
            else break;
        }
    }

    public boolean isFull(){
        return useSize==elem.length;
    }

    //删除元素
    public  void poll(){
        if(isEmpty()||useSize==1) return;
        swap(0,useSize-1);
        useSize--;
        siftDown(0,useSize);
    }

public boolean isEmpty(){
        return useSize==0;
}


}
